package com.acc.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.acc.entity.Admin;
import com.acc.entity.AdminRole;
import com.acc.entity.EasyUIDataGridJSON;
import com.acc.entity.Msg;
import com.acc.entity.Role;
import com.acc.entity.RoleName;
import com.acc.entity.RoleUri;
import com.acc.service.AuthorityService;
import com.acc.service.RoleService;
import com.acc.service.RoleUriService;
import com.acc.service.impl.AuthorityServiceImpl;
import com.acc.service.impl.RoleServiceImpl;
import com.acc.service.impl.RoleUriServiceImpl;
import com.alibaba.fastjson.JSON;
/**
 * 
 * @author 朱春阳
 *
 */
public class RoleUriController extends InnerDispacherController2 {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8128936181765400012L;

	private RoleUriService roleUriService = new RoleUriServiceImpl();

	/**
	 * 根据条件查询 查询对应的ROLE-URI
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void PageRoleUri(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Integer page = null;
		Integer rows = null;
		String roleName = request.getParameter("roleName");
		System.out.println(roleName);
		// 获取分页信息
		try {
			page = Integer.parseInt(request.getParameter("page"));
			rows = Integer.parseInt(request.getParameter("rows"));
		} catch (NumberFormatException nbf) {
		}
		Admin admin = (Admin) request.getSession().getAttribute("admin");
		// 取出session中管理员对应的权限信息
		List<RoleUri> pageAdmin = roleUriService.getPageRoleUri(roleName, page, rows);
		// 总记录数目
		int count = roleUriService.getAllByName(roleName);
		EasyUIDataGridJSON responseObject = new EasyUIDataGridJSON();
		
		responseObject.setTotal(count);
		responseObject.setRows(pageAdmin);

		String json = JSON.toJSONString(responseObject);
		response.setContentType("application/json");
		System.out.println(json);
		response.getWriter().append(json);
	}

	public void PageAllRoleUri(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Integer page = null;
		Integer rows = null;
		// 获取分页信息
		try {
			page = Integer.parseInt(request.getParameter("page"));
			rows = Integer.parseInt(request.getParameter("rows"));
		} catch (NumberFormatException nbf) {
		}
		Admin admin = (Admin) request.getSession().getAttribute("admin");
		// 取出session中管理员对应的权限信息
		List<RoleUri> pageAdmin = roleUriService.getPage(page, rows);
		// 总记录数目
		int count = roleUriService.getAll();
		EasyUIDataGridJSON responseObject = new EasyUIDataGridJSON();
		responseObject.setTotal(count);
		responseObject.setRows(pageAdmin);

		String json = JSON.toJSONString(responseObject);
		response.setContentType("application/json");
		response.getWriter().append(json);
	}

	/**
	 * 增加一条 ROLE-URI 记录
	 * 
	 * @param request
	 * @param response
	 */
	public void addOneRoleUri(HttpServletRequest request, HttpServletResponse response) {
		RoleUriService roleUriService = new RoleUriServiceImpl();

		// 从输入框中取出值
		String role = request.getParameter("role");
		String uri = request.getParameter("uri");

		// 将取出的值封装起来
		RoleUri roleuri = new RoleUri();
		roleuri.setRole(role);
		roleuri.setUri(uri);

		// 增加记录
		boolean flag = roleUriService.addRoleUri(roleuri);

		// 存储判断的信息
		Msg msg = new Msg();
		String result = null;
		if (flag) {
			msg.setErrorMsg(null);
			msg.setSuccess(true);
			result = JSON.toJSONString(msg);
		} else {
			msg.setErrorMsg("增加失败");
			msg.setSuccess(false);
			result = JSON.toJSONString(msg);
		}

		// 将结果返回到页面上
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.append(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}

	}

	/**
	 * 更新一个ROLE-URI关系
	 * 
	 * @param request
	 * @param response
	 */
	public void editOneRoleUri(HttpServletRequest request, HttpServletResponse response) {
		RoleUriService roleUriService = new RoleUriServiceImpl();

		String id = request.getParameter("role_id");

		String role = request.getParameter("role");

		String uri = request.getParameter("uri");

		Integer role_id = null;
		try {
			role_id = Integer.parseInt(id);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		roleUriService.updateRoleUri(role, uri, role_id);
	}

	public void PgaeListRole(HttpServletRequest request, HttpServletResponse response) {
		Integer page = null;
		Integer rows = null;
		try {
			page = Integer.parseInt(request.getParameter("page"));
			rows = Integer.parseInt(request.getParameter("rows"));
		} catch (NumberFormatException nfe) {
			nfe.printStackTrace();
		}

		RoleService roleService = new RoleServiceImpl();
		List<RoleName> rolenames = roleService.getAll();
		int count = rolenames.size();

		List<RoleName> pageRoleNames = roleService.getAllByPage(page, rows);

		EasyUIDataGridJSON json = new EasyUIDataGridJSON();
		json.setTotal(count);
		json.setRows(pageRoleNames);

		response.setContentType("utf-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.append(JSON.toJSONString(json));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			out.close();
		}
	}
}
